//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by Minghe on 11/6/15.
//  Copyright © 2015 Stanford swift. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()


        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        
        guard let objc = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) else
        {
            MHzLog("json中没有数据")
            addChildViewController("HomeViewController", imageName: "tabbar_home", title: "首页")
            
            addChildViewController("MessageViewController", imageName: "tabbar_message_center", title: "消息")
            
            addChildViewController("DiscoverViewController", imageName: "tabbar_discover", title: "广场")
            
            addChildViewController("MessageViewController", imageName: "tabbar_profile", title: "我")
            return

        }
        
        // 根据json返回的数据加载控制器
        for dict in objc as! [[String: AnyObject]]
        {
            addChildViewController(dict["vcName"] as! String, imageName: dict["imageName"] as! String, title: dict["title"] as! String)
        }
        
        
        /*
        do{
        // throws是 异常处理机制
            // do catch 一旦方法抛出异常，就会执行catch后面｛｝的内容，如果没有异常，catch后面｛｝的内容就不执行
            // try : 正常处理异常
            // try!: 强制处理异常（忽略异常），也就是说告诉系统一定不会发生异常，如果真的发生了异常，程序crash
            // try?: 忽略异常，告诉系统可能有异常，也可能没有异常，如果发生异常返回值就是nil,如果没有发生异常，会将返回值包装为一个可选类型
            let objc = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)
        
        }catch {
            
            
        }
        */

        

        
    }
    
    func addChildViewController(childControllerName: String, imageName: String, title: String) {
        
        // 0.获取命名空间
        guard let nsp = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else
        {
            MHzLog("没有获取到命名空间")
            return
        }
        
        // 1.通过一个字符串创建一个类
        let cls: AnyClass? = NSClassFromString(nsp + "." + childControllerName)
        
        // 2.通过一个Class创建一个对象
        guard let vcCls = cls as? UITableViewController.Type else
        {
            MHzLog("cls(AnyClass)没有值")
            return
        }
        
        let childContorller = vcCls.init()
        
        // 3.设置属性
        childContorller.tabBarItem.image = UIImage(named: imageName)
        childContorller.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        childContorller.title = title
        
        let nav = UINavigationController(rootViewController: childContorller)
        
        addChildViewController(nav)
        
    }
    
    
    
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
